package com.bed.lovewithpatientbed.controller;

import com.bed.lovewithpatientbed.common.bean.HospitalDetailBean;
import com.bed.lovewithpatientbed.common.bean.LayUiPageBean;
import com.bed.lovewithpatientbed.core.annocation.LogAnno;
import com.bed.lovewithpatientbed.pojo.*;
import com.bed.lovewithpatientbed.service.AddressService;
import com.bed.lovewithpatientbed.service.DealerService;
import com.bed.lovewithpatientbed.service.HospitalManagerService;
import com.bed.lovewithpatientbed.service.HospitalService;
import com.bed.lovewithpatientbed.util.LayUiUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("hospital")
public class HospitalManagerController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private AddressService addressService;

    @Autowired
    private HospitalManagerService hospitalManagerService;

    @Autowired
    private DealerService dealerService;

    @RequestMapping("list")
    public String list(){
        return "list1";
    }

    /**
         * @title 根据条件展示医院信息
         * @description 需要展示出医院地址信息，需要关联地区表进行展示
         * @author yg
         * @updateTime
         * @throws
         */
    @LogAnno(operateType = "条件组合查询，数据列表展示")
    @RequestMapping("listDo")
    @ResponseBody
    public Map<String,Object> hospitalList(LayUiPageBean pageBean, Hospital hospital, Double count1, Double count2,Address address,String province){


        Map<String,Object> resultMap=new HashMap<>();
        List<Hospital> hospitalList=null;
        List<HospitalDetailBean> hospitalDetailBeanList=new ArrayList<>();
        long countRow=0;

        hospitalList = hospitalService.getHospitalBySelect(pageBean,hospital,count1,count2,address);

        for (Hospital hosp:hospitalList
        ) {
            //封装为bean实体类
            HospitalDetailBean hospitalDetailBean=new HospitalDetailBean();
            hospitalDetailBean.setHospitalId(hosp.getHospitalId());
            hospitalDetailBean.setDeptId(hosp.getDeptId());
            hospitalDetailBean.setHospitalName(hosp.getHospitalName());
            //获取医院地址
            Address addressList = addressService.getById(hosp.getAddressId());
            String hospitalName="";
            hospitalName=hospitalName+addressList.getProvince()+addressList.getCity()+addressList.getDistrict();
            hospitalDetailBean.setAddressId(hospitalName);
            //获取医院对接人名字
            HospitalManager hospitalManager = hospitalManagerService.selectById(hosp.getHospitalManagerId());
            String hospitalManagerName="";
            hospitalManagerName=hospitalManagerName+hospitalManager.getHospitalManagerName();
            hospitalDetailBean.setHospitalManagerId(hospitalManagerName);

            //获取经销商名字
            Dealer dealer = dealerService.selectDealer(hosp.getDealerId());
            String dealerName="";
            dealerName=dealerName+dealer.getDealerName();
            hospitalDetailBean.setDealerId(dealerName);

            hospitalDetailBean.setHospitalCard(hosp.getHospitalCard());
            hospitalDetailBean.setHospitalRent(hosp.getHospitalRent());
            hospitalDetailBean.setHospitalGainsharing(hosp.getHospitalGainsharing());
            hospitalDetailBeanList.add(hospitalDetailBean);
        }
        countRow = hospitalService.countRow();

        return LayUiUtil.getLayTableResponseMap("success",0,countRow,hospitalDetailBeanList);

    }

    @LogAnno(operateType = "新增传查询数据")
    @RequestMapping("add")
    @ResponseBody
    public Map<String,Object> add(){
        Map<String,Object> resultMap=new HashMap<>();
        List<HospitalManager> hsmanagerList = hospitalManagerService.getAll();
        System.out.println(hsmanagerList);
        List<Dealer> dealerList = dealerService.getAll();
        resultMap.put("hsmanagerList",hsmanagerList);
        resultMap.put("dealerList",dealerList);
        return resultMap;
    }

    @LogAnno(operateType = "执行新增")
    @RequestMapping("addDo")
    @ResponseBody
    public Map<String,Object> addHospital(Address address,HospitalDetailBean hospitalDetailBean){
        System.out.println(address);
        System.out.println(hospitalDetailBean);
        Map<String,Object> resultMap=new HashMap<>();
        try{
            int i=0;
            //新增地址信息并得到地址id
            int addressid = addressService.insertAddress(address);
            int hospitalManagerId=Integer.valueOf(hospitalDetailBean.getHospitalManagerId());
            int dealerId=Integer.valueOf(hospitalDetailBean.getDealerId());
            //封装新增医院的信息
            Hospital hospital=new Hospital();
            hospital.setHospitalName(hospitalDetailBean.getHospitalName());
            hospital.setDealerId(dealerId);
            hospital.setSoftdel(1);
            hospital.setAddressId(addressid);
            hospital.setHospitalCard(hospitalDetailBean.getHospitalCard());
            hospital.setHospitalGainsharing(hospitalDetailBean.getHospitalGainsharing());
            hospital.setHospitalRent(hospitalDetailBean.getHospitalRent());
            hospital.setHospitalManagerId(hospitalManagerId);

            int addHospital = hospitalService.addHospital(hospital);


            i=i+addHospital;

            if(i>0){
                resultMap.put("result",true);
            }else {
                resultMap.put("result",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }

    @LogAnno(operateType = "执行删除")
    @RequestMapping("delDo")
    @ResponseBody
    public Map<String,Object> delbatchs(Integer[] ids){
        System.out.println(ids);
        Map<String,Object> resultMap=new HashMap<>();
        try{
            int i = hospitalService.delbatchHospital(ids);
            if(i>0){
                resultMap.put("result",true);
            }else {
                resultMap.put("result",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }

    @RequestMapping("update")
    public String update(){
        return "update";
    }

    @LogAnno(operateType = "执行修改")
    @RequestMapping("updateDo")
    @ResponseBody
    public Map<String,Object> updateHospital(HospitalDetailBean hospitalDetailBean){
        System.out.println(hospitalDetailBean);
        Map<String,Object> resultMap=new HashMap<>();

        int hospitalManagerId=Integer.valueOf(hospitalDetailBean.getHospitalManagerId());
        int dealerId=Integer.valueOf(hospitalDetailBean.getDealerId());

        Hospital hospital=new Hospital();
        hospital.setHospitalId(hospitalDetailBean.getHospitalId());
        hospital.setHospitalName(hospitalDetailBean.getHospitalName());
        hospital.setDealerId(dealerId);
        hospital.setHospitalCard(hospitalDetailBean.getHospitalCard());
        hospital.setHospitalGainsharing(hospitalDetailBean.getHospitalGainsharing());
        hospital.setHospitalRent(hospitalDetailBean.getHospitalRent());
        hospital.setHospitalManagerId(hospitalManagerId);
        try{
            int i = hospitalService.updateHospital(hospital);
            if(i>0){
                resultMap.put("result",true);
            }else {
                resultMap.put("result",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }

    @RequestMapping("HospitalInfo")
    public Map<String,Object> HospitalInfo(Integer hid){
        Map<String,Object> resultMap=new HashMap<>();
        try{
            Hospital hospital = hospitalService.selectById(hid);
            resultMap.put("hospital",hospital);
            resultMap.put("result",true);
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }

    @LogAnno(operateType = "查找省份列表")
    @RequestMapping("provincelist")
    @ResponseBody
    public List<Address> provincelist(){

        List<Address> addressList = addressService.getProvince();
        return  addressList;
    }

    @LogAnno(operateType = "查找地市列表")
    @RequestMapping("citylist")
    @ResponseBody
    public List<Address> citylist(String province){
        System.out.println(province);
        List<Address> citylist=addressService.getCity(province);
        System.out.println(citylist);
        return citylist;
    }

    @LogAnno(operateType = "查找县区列表")
    @RequestMapping("districtlist")
    @ResponseBody
    public List<Address> districtlist(String province,String city){
        List<Address> distinctlist = addressService.getDistinct(province,city);
        System.out.println(distinctlist);
        return  distinctlist;
    }

    /**
         * @title 查询所有医院对接人信息
         * @description
         * @author yg
         * @updateTime
         * @throws
         */

}

